﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using DMTroube_Model;
using DMTroube_Model.Msg;
using HtmlAgilityPack;

namespace DMTroube_Manager.Info
{
    public class InfoAnalysis
    {
        private static HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlDocument();
        public static void init(string resultJson, Action<object?, ResultAnalysisInfo>? action1, Action<object?, ResultAnalysisInfo>? action2)
        {
            if (resultJson != null && resultJson != "null")
            {
                ResultWebMsg? result = JsonSerializer.Deserialize<ResultWebMsg>(resultJson);
                if (result != null)
                {
                    if (result.Type == "info")
                    {
                        ResultAnalysisInfo resultAnalysis = LeftMsg(result.Html);
                        action1?.Invoke(null, resultAnalysis);
                    }
                }
            }
        }
        private static ResultAnalysisInfo LeftMsg(string? html)
        {
            ResultAnalysisInfo resultAnalysis = new ResultAnalysisInfo();
            if (string.IsNullOrWhiteSpace(html) || html.StartsWith("null")) { return resultAnalysis; }
            htmlDocument.LoadHtml(html);
            var imgNode = htmlDocument.DocumentNode.SelectSingleNode("//img");
            var nameNode = htmlDocument.DocumentNode.SelectSingleNode("//span[@class='u2QdU6ht']");
            var contentTextNode = htmlDocument.DocumentNode.SelectSingleNode("//span[@class='webcast-chatroom___content-with-emoji-text']");
            var contentImgNode = htmlDocument.DocumentNode.SelectSingleNode("//img[@class='sG3bJJhR']");
            var contentNumNode = htmlDocument.DocumentNode.SelectSingleNode("//span[@class='xvIN8CM4']");
            var tagNode = htmlDocument.DocumentNode.SelectSingleNode("//span[@class='GDJPyGRR']");
            int level = 0; string name = ""; string contentText = ""; int giftNum = 0; string giftName = ""; string tag = "无";

            #region 等级
            string levelSrc = imgNode != null ? imgNode.Attributes["src"] != null ? imgNode.Attributes["src"].Value : "未知" : "0";
            if (levelSrc.Contains("level_v1_"))
            {
                string[] levelInfos = levelSrc.Replace("level_v1_", "|").Split("|");
                string levelinfo = levelInfos.Length > 1 ? levelInfos[1].Substring(0, 2) : "0";
                level = levelinfo.Contains(".") ? Convert.ToInt16(levelinfo.Replace(".", "")) : Convert.ToInt16(levelinfo);
            }
            #endregion

            //昵称
            name = nameNode != null ? nameNode.InnerText : "未知";
            //评论内容 
            contentText = contentTextNode != null ? contentTextNode.InnerText : "未知";
            //礼物数量
            giftNum = contentNumNode != null ? Convert.ToInt16(contentNumNode.InnerText.Replace("&nbsp;×&nbsp;", "")) : giftNum;
            //粉丝灯牌
            tag = tagNode != null ? tagNode.InnerText : "无";

            #region 礼物名称
            giftName = "未知";
            #endregion

            string msg = "";

            if (contentImgNode != null)
            {
                resultAnalysis.Type = 0;

            }
            else if (contentTextNode != null)
            {
                resultAnalysis.Type = 1;
            }
            resultAnalysis.Level = level;
            resultAnalysis.FanGroup = tag;
            resultAnalysis.UserName = name;
            resultAnalysis.GiftNum = giftNum;
            resultAnalysis.Gift = giftName;
            return resultAnalysis;
        }
        private static void RightMsg()
        {

        }



    }
}
